﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.Presenters.Stock;
using WarehouseManagement.ViewModels.Stock;
using System.Linq;
using DevExpress.XtraSplashScreen;

namespace WarehouseManagement.Views.Stock
{
    public partial class StockByQuantityAndValue : XtraForm, IView<ViewStockByQtyAndValuePresenter>, IViewStockByQuantityAndValueViewModel
    {
        private ViewStockByQtyAndValuePresenter _presenter;

        public StockByQuantityAndValue()
        {
            InitializeComponent();
            _presenter = SetupPresenter();
        }

        private void cboOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssignDate();
        }

        private void AssignDate()
        {
            var today = DateTime.Now;
            switch (cboOption.SelectedIndex)
            {
                case 1:
                    {
                        DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));
                        this.FromDate = startOfWeek;
                        this.ToDate = startOfWeek.AddDays(6);
                        break;
                    }
                case 0:
                    {
                        this.FromDate = today;
                        this.ToDate = FromDate;
                        break;
                    }
                case 2:
                    {
                        this.FromDate = new DateTime(today.Year, today.Month, 1);
                        this.ToDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
                        break;
                    }
                case 3:
                    {
                        var period = GetPeriod(today.Month);
                        this.FromDate = GetStartDayOfPeriod(period, today.Year);
                        var todate = this.FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 4:
                    {
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 5:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 1, DateTime.DaysInMonth(today.Year, 1));
                        break;
                    }
                case 6:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 2, 1);
                        this.ToDate = new DateTime(today.Year, 2, DateTime.DaysInMonth(today.Year, 2));
                        break;
                    }
                case 7:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 3, 1);
                        this.ToDate = new DateTime(today.Year, 3, DateTime.DaysInMonth(today.Year, 3));
                        break;
                    }
                case 8:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 4, 1);
                        this.ToDate = new DateTime(today.Year, 4, DateTime.DaysInMonth(today.Year, 4));
                        break;
                    }
                case 9:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 5, 1);
                        this.ToDate = new DateTime(today.Year, 5, DateTime.DaysInMonth(today.Year, 5));
                        break;
                    }
                case 10:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 6, 1);
                        this.ToDate = new DateTime(today.Year, 6, DateTime.DaysInMonth(today.Year, 6));
                        break;
                    }
                case 11:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 7, 1);
                        this.ToDate = new DateTime(today.Year, 7, DateTime.DaysInMonth(today.Year, 7));
                        break;
                    }
                case 12:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 8, 1);
                        this.ToDate = new DateTime(today.Year, 8, DateTime.DaysInMonth(today.Year, 8));
                        break;
                    }
                case 13:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 9, 1);
                        this.ToDate = new DateTime(today.Year, 9, DateTime.DaysInMonth(today.Year, 9));
                        break;
                    }
                case 14:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 10, 1);
                        this.ToDate = new DateTime(today.Year, 10, DateTime.DaysInMonth(today.Year, 10));
                        break;
                    }
                case 15:
                    {
                        // Thang 11
                        this.FromDate = new DateTime(today.Year, 11, 1);
                        this.ToDate = new DateTime(today.Year, 11, DateTime.DaysInMonth(today.Year, 11));
                        break;
                    }
                case 16:
                    {
                        // Thang 12
                        this.FromDate = new DateTime(today.Year, 12, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 17:
                    {
                        // Quy 1
                        this.FromDate = GetStartDayOfPeriod(1, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 18:
                    {
                        // Quy 2
                        this.FromDate = GetStartDayOfPeriod(2, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 19:
                    {
                        // Quy 3
                        this.FromDate = GetStartDayOfPeriod(3, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 20:
                    {
                        // Quy 4
                        this.FromDate = GetStartDayOfPeriod(4, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
            }
        }

        private int GetPeriod(int month)
        {
            if (month >= 1 && month <= 3)
                return 1;
            else if (month >= 4 && month <= 6)
                return 2;
            else if (month >= 7 && month <= 9)
                return 3;
            else
                return 4;
        }

        private DateTime GetStartDayOfPeriod(int period, int year)
        {
            if (period < 1 || period > 4)
                throw new ArgumentException("Period invalid");
            return new DateTime(year, (period * 3) - 2, 1);
        }

        public ViewStockByQtyAndValuePresenter SetupPresenter()
        {
            var p = new ViewStockByQtyAndValuePresenter();
            p.RegisterViewModel(this);
            return p;
        }

        public DateTime FromDate
        {
            get
            {
                return txtFromDate.DateTime;
            }
            set
            {
                txtFromDate.DateTime = value;
            }
        }

        public DateTime ToDate
        {
            get
            {
                return txtToDate.DateTime;
            }
            set
            {
                txtToDate.DateTime = value;
            }
        }

        private void StockByQuantityAndValue_Load(object sender, EventArgs e)
        {
            cboOption_SelectedIndexChanged(sender, e);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            _presenter.LoadStockList();
            SplashScreenManager.CloseForm();
        }

        public IEnumerable<ViewStockDTO> StockList
        {
            get
            {
                return grdStock.DataSource as IEnumerable<ViewStockDTO>;
            }
            set
            {
                grdStock.DataSource = value;
            }
        }

        private void txtFromDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                ToDate = FromDate;
        }

        private void txtToDate_EditValueChanged(object sender, EventArgs e)
        {
            if (ToDate < FromDate)
                FromDate = ToDate;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            btnView_Click(this, e);

            var rptPO = new rptStock();
            rptPO.Parameters["Title"].Value = string.Format("Báo cáo tồn kho từ {0:dd/MM/yyyy} đến {1:dd/MM/yyyy}", FromDate, ToDate).ToUpper();
            rptPO.Parameters["Title"].Visible = false;
            rptPO.Parameters["ReportDate"].Value = DateTime.Now;
            rptPO.Parameters["ReportDate"].Visible = false;

            using (ReportPrintTool printTool = new ReportPrintTool(rptPO))
            {
                // Invoke the Print Preview form modally, 
                // and load the report document into it.
                rptPO.DataSource = this.StockList.ToList();
                printTool.ShowPreviewDialog();

                // Invoke the Print Preview form
                // with the specified look and feel setting.
                printTool.ShowPreview(UserLookAndFeel.Default);
            }
        }
    }
}